package com.yzd.learnjava.strings;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.yzd.learnjava.util.Print;
/*
 * Matcher/Pattern
 */
public class StartEnd {

	public static String input = "As long there is injustice , whenever a\n"
			+ "Targathian baby by cries out, whenever a distress"
			+ "signal sounds among the stars ... we'll be there.\n"
			+ "This fine ship, and this fine crew ... \n"
			+ "never give up! Never Surrensder!";
	
	private static class Display{
		private boolean regexPrinted = false ;
		private String regex ;
		public Display(String regex) {
			this.regex = regex;
		}
		
		void display(String msg) {
			if(!regexPrinted) {
				Print.print("regex : " + regex);
				regexPrinted = true;
			}
			Print.print(msg);
		}
	}
	
	static void examine(String s , String regex) {
		Display d = new Display(regex);
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(s);
		int i = 0;
		while(m.find()) {
			d.display("find() '" + m.group() + "' start = " + m.start() + ", end = " + m.end());
		}
		if(m.lookingAt()) {
			d.display("lookingAt()  start = " + m.start() + ", end = " + m.end());
		}
		
		if(m.matches()) {
			d.display("matches()  start = " + m.start() + ", end = " + m.end());
		}
	}
	public static void main(String[] args) {
		for(String in : input.split("\n")) {
			Print.print("input : " + in);
			for(String regex : new String[] {"\\w*ere\\w*", "\\w*ever", "T\\w+","Never.*?!"}) {
				examine(in, regex);
			}
		}

	}

}
